Skip to main content

Writing Scripts

AREX allows you to add dynamic behavior to requests and collections through scripting (JavaScript code snippets).

What is scripts used for?

  1. Test (assert) the correctness of the request return result (test script).
  2. Dynamically modifying interface request parameters, such as adding interface signature parameters, etc. (pre-request script)
  3. Passing data between interface requests (using scripts to manipulate variables).

You can add JavaScript code to execute during two events in the flow:

  1. Before a request is sent to the server, as a pre-request script under the Pre-request Script tab.
  2. After a response is received, as a test script under the Tests tab.

You can add pre-request and test scripts to a collection, a folder, or a single request within a collection:

  • Set pre-request or test scripts for the entire collection or a folder under the collection. Once set, they will run for every request in the collection or direct child request in the folder.
  • Set pre-request or test scripts for individual requests. Once set, they will run only for a single request.

Execution order of scripts

The script execution order for a single request looks like this:

  1. A pre-request script associated with a request will execute before the request is sent.
  2. A test script associated with a request will execute after the request is sent.

For every request in a collection, scripts will execute in the following order:

  1. A pre-request script associated with a collection will run prior to every request in the collection.
  2. A pre-request script associated with a folder will run prior to every direct child request in the folder.
  3. A test script associated with a collection will run after every request in the collection.
  4. A test script associated with a folder will run after every direct child request in the folder.

Writing pre-request scripts

Re-using pre-request scripts (to do)

  1. Select Collection in the sidebar.

  2. Select the collection or the folder you want to add Re-using pre-request scripts.

  3. Select the Pre-request Scripts tab >> Add Script Block >> CustomScript.

添加集合脚本

Enter code that will run before every request in the collection or direct child request in the folder.

集合脚本

Scripting before your request runs

  1. Open the request, then select the Pre-request Script tab.

  2. Select Add Script Block and enter the JavaScript you need to process before the request runs, then select Save.

  3. Select Send to send the request. The code will execute before Postman sends the request to the API.

请求添加前置脚本

Writing test scripts

You can use test scripts to assert whether your API is working as expected or not, and set the result data returned by the request as environment variables, etc.

Validating responses

  1. open the request and select the Tests tab.

    添加后置脚本

  2. Select Add Script Block and enter your code for your request.

  3. Select Send and the tests will execute after the request runs.

  4. The output is in the response's Result tab.

    断言结果

Reference

1. Using variables in scripts

Environment variables

Your scripts can use the pm.environment methods to access and manipulate variables in the active (currently selected) environment.

  1. Set the variable with the specified name and value in the active environment:
pm.environment.set("variable_key", "variable_value");
  1. Get the variable with the specified name in the active environment:
pm.environment.get("variable_key");
  1. Delete the variable:
pm.environment.delete("variable_key");

Temporary variables

  1. Set the temporary variables with the specified name and value:
pm.variables.set("variable_key", "variable_value")
  1. Get the temporary variables:
pm.variables.get("variable_key")
  1. Delete the temporary variables:
pm.variables.delete("variable_key")

2. Sending requests from scripts

  1. Sending GET requests
let response = await pm.sendRequest({method:"GET",url:"http://10.5.153.1:8090/api/config/schedule/useResult/appId/arex-0.2.4.test2"});  
  1. Sending POST requests
let response = await pm.sendRequest({url:"http://10.5.153.1:8088/api/report/queryDifferences",method:"POST",data:"{"categoryName":"ServletEntrance","operationName":"/owners/{ownerId}","planItemId":"633184edc9af0157f44eaeba"}",headers:{"Content-Type":"application/json","access-token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbmZvIjoidGVzdCJ9.YeLmUW--fqrtmag1QTDmL8U7RVZlb34xPAAxorxSCPM"}});  

3. Validating responses

Test if the response status code is 200:

pm.test("Status code is 200", ()=> {
pm.expect(pm.response.status).toBe(200);
});

Test if the age in the result messages is 18:

pm.test("Check JSON response property", ()=> {
pm.expect(pm.response.body.age).toBe(18);
});